package com.asjy.controller;

import com.asjy.po.*;
import com.asjy.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.MonitorInfo;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/16
 * @Description: com.asjy.controller
 * @version: 1.0
 */
@RequestMapping("/order")
@Controller
public class OrderController {
    @Resource
    private HttpSession session;
    @Resource
    private OrderService orderService;

    @RequestMapping("/toConfirmOrder")
    public String toConfirmOrder() {

        return "confirmOrder";
    }

    @RequestMapping("/testEl")
    public String testEl(Model model) {
        Map<Long, String> maps = new HashMap<>();
        maps.put(1L, "aa");
        maps.put(2L, "ba");
        maps.put(3L, "ca");
        model.addAttribute("maps", maps);
        return "el";
    }

    @RequestMapping("/toPay")
    public String toPay(Order order, Model model) {
        User user = (User) session.getAttribute("user");
        Cart cart = (Cart) session.getAttribute("cart");


        //1.保存订单信息
        order.setuId(user.getuId());
        order.setState(0);
        order.setOrderTime(new Date());
        order.setTotal(cart.getTotal());
        Integer oId = orderService.saveOrderAndItem(order, cart);

        //订单持久化后，需要清空购物车
        session.removeAttribute("cart");
        model.addAttribute("oId", oId);
        return "pay";
    }

    @RequestMapping("/doPay")
    public String doPay(Integer oId, Model model) {

        //对接网银接口，完成支付工作
        //成功
        Order order = new Order();
        order.setoId(oId);
        order.setState(1);
        orderService.changeState(order);
        OrderExt orderExt = orderService.getOrderById(oId);
        model.addAttribute("orderExt", orderExt);
        return "paySuccess";

    }

    @RequestMapping("/toOrder")
    //Transactional使用事务，但只处理运行时异常，默认情况下编译异常不处理
    public String toOrder(OrderPageBean orderPageBean, Model model) {
        User user = (User) session.getAttribute("user");

        Integer uId = user.getuId();
        orderPageBean.setuId(uId);
        OrderPageBean orderForPageBean = orderService.getOrdersByUid(orderPageBean);
        Map<Long, Integer> stateMap = orderService.getOrderCountByState(uId);
        model.addAttribute("stateMap", stateMap);
        model.addAttribute("orderPageBean", orderForPageBean);
        return "myOrder";

    }

    @RequestMapping("/delOrder")
    public String delOrder(Order order, Integer pageNow) {//2000

        order.setState(-1);
        orderService.changeState(order);//oid state=-1
        //“forward:toOrder” “redirect:/order/toOrder” 不走视图解析器，redirect地址必须写全路径
        //return "forward:toOrder?paegNow="+pageNow;
        return "redirect:/order/toOrder?pageNow=" + pageNow;
    }

    @RequestMapping("/doRecive") //2000
    public String doRecive(Order order, Integer pageNow) {

        order.setState(3);
        //oid state=-1
        orderService.changeState(order);
        //“forward:toOrder” “redirect:/order/toOrder” 不走视图解析器，redirect地址必须写全路径
        //return "forward:toOrder?paegNow="+pageNow;
        return "redirect:/order/toOrder?pageNow=" + pageNow;
    }

}